﻿using DevTeam;


using System.Collections.Generic;
using System.Data;

namespace ConfExpert.Forms.ImportWizards.Siparis
{
    public partial class Saks : XISiparis
    {
        public Saks()
            : base()
        {
            InitializeComponent();
            this.FileDone += this_FileDone;
        }

        public class cM
        {
            public int pos;
            public string name;
            public cM(int pos, string name)
            {
                this.pos = pos;
                this.name = name;
            }
        }

        void this_FileDone(DataSet ds, string FileName)
        {
            DataTable dtx = new DataTable("sip");
            dtx.Columns.Add("Model", typeof(string));
            dtx.Columns.Add("Taglia", typeof(string));
            for (int i = 1; i < 64; i++)
                dtx.Columns.Add(string.Format("x{0:d2}",i), typeof(int));

            DataTable dtStore = new DataTable("store");
            dtStore.Columns.Add("Nr", typeof(int));
            dtStore.Columns.Add("Name", typeof(string));

            foreach (DataTable dt in ds.Tables)
            {
                DataRowCollection rows = dt.Rows;
                DataColumnCollection cols = dt.Columns;

                if (rows.Count > 1)
                {

                    Dictionary<int, cM> Magaza = new Dictionary<int, cM>();
                    int start = 0;
                    for (int r = 0; r < dt.Rows.Count && start < 1; r++)
                        for (int c = 0; c < dt.Columns.Count; c++)
                            if (dt.Rows[r][c].ToStr() == "1")
                            {
                                for (int c2 = c; c2 < dt.Columns.Count; c2++)
                                    if (dt.Rows[r][c2].ToInt() > 0)
                                        if (!Magaza.ContainsKey(dt.Rows[r][c2].ToInt()))
                                        {
                                            int Nr = dt.Rows[r][c2].ToInt();
                                            string Name = dt.Rows[r + 1][c2].ToStr() + " " + dt.Rows[r + 2][c2].ToStr();
                                            Magaza.Add(Nr, new cM(c2, Name));

                                            DataRow dr = dtStore.NewRow();
                                            dr["Nr"] = Nr;
                                            dr["Name"] = Name;
                                            dtStore.Rows.Add(dr);
                                        }
                                start = r + 1;
                                break;
                            }


                    int start2 = 0, posBeden = 0, posModel = 0;
                    for (int c = 0; c < dt.Columns.Count && start2 < 1; c++)
                        for (int r = start; r < dt.Rows.Count; r++)
                            if (dt.Rows[r][c].ToStr().StartsWith("BALANCE"))
                            {
                                start2 = r;
                                posModel = c - 3;

                                for (int c2 = c; c2 < dt.Columns.Count; c2++)
                                    if (dt.Rows[r][c2].ToStr() == "Full line U")
                                        posBeden = c2 + 1;
                                break;
                            }

                    string Model = string.Empty;
                    for (int r = start2; r < dt.Rows.Count; r++)
                    {
                        if (dt.Rows[r][posModel].ToStr() != string.Empty)
                            Model = dt.Rows[r][posModel].ToStr();
                        if (dt.Rows[r][posBeden].ToStr() != string.Empty)
                        {
                            DataRow dr = dtx.NewRow();
                            dr["Model"] = Model;
                            dr["Taglia"] = dt.Rows[r][posBeden].ToStr();

                            foreach (KeyValuePair<int, cM> p in Magaza)
                                if (dt.Rows[r][p.Value.pos].ToInt() > 0)
                                    dr[string.Format("x{0:d2}", p.Key)] = dt.Rows[r][p.Value.pos].ToInt();

                            dtx.Rows.Add(dr);
                        }
                    }
                }
            }

            string str = dtx.Serialize();
            string str2 = dtStore.Serialize();

            MasterGrid.Init("GetSaksFormat", str, str2);
            MasterGrid.BestFitColumns();
        }

    }
}
